import http from '@/utils/http'

export const mixin = {
  data: function () {
    return {
      department: null,
      total: 10,
      loading: false
    }
  },
  created: function () {},
  methods: {
    fetchDepartment() {
      this.loading = true
      http.getDepartment().then((res) => {
        // todo ****** 方便后面的数据处理，默认加一级根节点 *****
        const treeNode = [
          {
            children: res.data,
            createTime: '2018-01-22 19:00:23',
            id: '-1',
            isLock: false,
            name: '根部门',
            disabled: true,
            parentId: 99999999,
            weight: 1
          }
        ]

        /**
         * @description 建立节点引用关系方便后面的引用查找
         */
        const establishRef = (arr, parent) => {
          for (let i = 0; i < arr.length; i++) {
            const node = arr[i]
            parent && (node['parent'] = parent)
            if (Array.isArray(node.children)) {
              establishRef(node.children, node)
            }
          }
        }
        establishRef(treeNode)
        this.department = treeNode

        this.total = res.data.length

        this.loading = false
      })
    }
  }
}
